package com.rpoonolly.simplegame.editor;

import com.rpoonolly.simplegame.Config;
import com.rpoonolly.simplegame.core.aabb.AABBEntity;
import com.rpoonolly.simplegame.textures.TextureLoader;
import com.rpoonolly.simplegame.worldGrid.GridCoordinate;
import com.rpoonolly.simplegame.worldGrid.WorldGridGameMap;
import com.rpoonolly.simplegame.worldGrid.WorldGridRenderer;
import com.rpoonolly.simplegame.worldGrid.WorldGridTile;

public class WorldGridEditRenderer extends WorldGridRenderer {
	private AABBEntity selectionMarker;
	
	public WorldGridEditRenderer(WorldGridGameMap worldMap, AABBEntity player, TextureLoader textureLoader) {
		super(worldMap, player, textureLoader);
		
		selectionMarker = new AABBEntity();
	}
	
	@Override
	public void render() {
		super.render();
		
		if (Config.isEditModeEnabled.get()) {
			int focalX = (int) focalPoint.x;
			int focalY = (int) focalPoint.y;
			
			selectionMarker.setX(focalX - (focalX % worldGrid.getGridCellWidth()));
			selectionMarker.setY(focalY - (focalY % worldGrid.getGridCellHeight()));
			
			WorldGridTile selectedTile = worldGrid.getCollidingTiles(selectionMarker).get(0);
			renderSelectedTile(selectedTile);
		}
	}
	
	private void renderSelectedTile(WorldGridTile tile) {
		GridCoordinate tileCoord = tile.getCoordinate();
		
		int width = worldGrid.getGridCellWidth();
		int height = worldGrid.getGridCellHeight();
		int x = tileCoord.getX() * width;
		int y = tileCoord.getY() * height;

		double xcoord = x - camera.getX();
		double ycoord = -1 * ((y + height) - (camera.getY() + camera.getHeight()));
		
		graphics.drawRect((int) xcoord, (int) ycoord, width, height);
	}
}
